<?php

/**
 * @author thanhansoft
 * @copyright 2010
 */

if ( !defined('IN_SITE') ){
	die('Hacking Attemp!');
}

function exec_string($string){
    $db = new DBConnection();
    $db->rq($string);
}
function exec_string_lastid($string){
    $db = new DBConnection();
    $db->rq($string);
    return $db->last_id();
}
function return_true_false($query){
    $db = new DBConnection();
    $res = $db->rq($query);
    $num_row = $db->num_rows($res);
    if($num_row > 0){
        return true;
    }
    return false;
}
function return_array($query){
    $row = array();
    $db = new DBConnection();
    $res = $db->rq($query);
    $num_row = $db->num_rows($res);
    if($num_row > 0){
        $row = $db->fetch($res);
    }
    return $row;
}
function return_array_multi($query){
    $rows = array();
    $db = new DBConnection();
    $res = $db->rq($query);
    $num_row = $db->num_rows($res);
    if($num_row > 0){
        $k = 1;
        while($row = $db->afetch($res)){
            $rows[$k] = $row;
            $k++;
        }
    }
    return $rows;
}
function cut_string($string, $max_length){
	if($string && $max_length){
	   if (strlen($string) > $max_length){
       		$string = substr($string, 0, $max_length);
       		$pos = strrpos($string, " ");
		    if($pos === false) {
               return substr($string, 0, $max_length)."...";
           	}
       		return substr($string, 0, $pos)."...";
   		}else{
       		return $string;
   		}
	}
}
function g3_pagination($base_url, $num_items, $per_page, $start_item, $pos_var = 'start' , $add_prevnext_text = '//trước//tiếp theo') {
	$page_string = '';
	$total_pages = ceil($num_items/$per_page);

	if ( $total_pages > 1 ) {
		$on_page = floor($start_item / $per_page) + 1;
		if ( $total_pages > 10 ) {
			$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
			for($i = 1; $i < $init_page_max + 1; $i++) {
				$page_string .= ( $i == $on_page ) ? '<b class="pagecureent">' . $i . '</b>' : '<a class="pagenav" href="' . ($base_url . "$pos_var" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
				if ( $i <  $init_page_max ) {
					$page_string .= ", ";
				}
			}
			if ( $total_pages > 3 ) {
				if ( $on_page > 1  && $on_page < $total_pages ) {
					$page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
					$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
					$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
					for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {
						$page_string .= ($i == $on_page) ? '<b class="pagecureent">' . $i . '</b>' : '<a class="pagenav" href="' . ($base_url . "$pos_var" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
						if ( $i <  $init_page_max + 1 ) {
							$page_string .= ', ';
						}
					}
					$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
				}
				else {
					$page_string .= ' ... ';
				}

				for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {
					$page_string .= ( $i == $on_page ) ? '<b class="pagecureent">' . $i . '</b>'  : '<a class="pagenav" href="' . ($base_url . "$pos_var" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
					if ( $i <  $total_pages ) {
						$page_string .= ", ";
					}
				}
			}
		}
		else {
			for($i = 1; $i < $total_pages + 1; $i++) {
				$page_string .= ( $i == $on_page ) ? '<b class="pagecureent">' . $i . '</b>' : '<a class="pagenav" href="' . ($base_url . "$pos_var" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
				if ( $i <  $total_pages ) {
					$page_string .= ' ';
				}
			}
		}

		if ( $add_prevnext_text ) {
			list ($lang_page, $lang_prev, $lang_next) = explode ('//', $add_prevnext_text);
			if ( $on_page > 1 ) {
				$page_string = ' <a class="pageactive" href="' . ($base_url . "$pos_var" . ( ( $on_page - 2 ) * $per_page ) ) . '">' . $lang_prev . '</a>&nbsp;&nbsp;' . $page_string;
			}
			if ( $on_page < $total_pages ) {
				$page_string .= '&nbsp;&nbsp;<a class="pageactive" href="' . ($base_url . "$pos_var" . ( $on_page * $per_page ) ) . '">' . $lang_next . '</a>';
			}
			$page_string = $lang_page . $page_string;
		}
	}
	return $page_string;
}
function pagination($base_url, $num_items, $per_page, $start_item, $pos_var = 'start' , $add_prevnext_text = 'Xem trang: //trÆ°á»>c//tiáº¿p theo') {
	$page_string = '';
	$total_pages = ceil($num_items/$per_page);

	if ( $total_pages > 1 ) {
		$on_page = floor($start_item / $per_page) + 1;
		for($i = 1; $i < $total_pages + 1; $i++) {
			$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>'  : '<a href="' . ($base_url . "&amp;$pos_var=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
			if ( $i <  $total_pages ) {
				$page_string .= ', ';
			}
		}
		$page_string .= '</select>';

		if ( $add_prevnext_text ) {
			list ($lang_page, $lang_prev, $lang_next) = explode ('//', $add_prevnext_text);
			if ( $on_page > 1 ) {
				$page_string = ' <a href="' . ($base_url . "&amp;$pos_var=" . ( ( $on_page - 2 ) * $per_page ) ) . '">' . $lang_prev . '</a>&nbsp;&nbsp;' . $page_string;
			}
			if ( $on_page < $total_pages ) {
				$page_string .= '&nbsp;&nbsp;<a href="' . ($base_url . "&amp;$pos_var=" . ( $on_page * $per_page ) ) . '">' . $lang_next . '</a>';
			}
			$page_string = $lang_page . $page_string;
		}
	}
	return $page_string;
}
function ConvertNonUni($text){
    $text = str_replace( 
		array('-','  ',' ','%',"/","\\",'"','?','<','>',"#","^","`","'","=","!",":" ,",,","..","*","&","__", "@", ".", "$","(",")", ",", ";", "|", "[", "]", "{", "}","~","+","“","”"), 
		array('',' ','-','' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'','' ,'','' ,'' ,'' , "" ,"" ,"", "", "", "", "","", "", "", "", "", "", "", "","", "","",""), $text); 
		$chars = array("a","A","e","E","o","O","u","U","i","I","d", "D","y","Y"); 
		$uni[0] = array("á","à","ạ","ả","ã","â","ấ","ầ", "ậ","ẩ","ẫ","ă","ắ","ằ","ặ","ẳ"); 
		$uni[1] = array("Á","À","Ạ","Ả","Ã","Â","Ấ","Ầ", "Ậ","Ẩ","Ẫ","Ă","Ắ","Ằ","Ặ","Ẳ"); 
		$uni[2] = array("é","è","ẹ","ẻ","ẽ","ê","ế","ề" ,"ệ","ể","ễ"); 
		$uni[3] = array("É","È","Ẹ","Ẻ","Ẽ","Ê","Ế","Ề" ,"Ệ","Ể","Ễ"); 
		$uni[4] = array("ó","ò","ọ","ỏ","õ","ô","ố","ồ", "ộ","ổ","ỗ","ơ","ớ","ờ","ợ","ở", "ỡ"); 
		$uni[5] = array("Ó","Ò","Ọ","Ỏ","Õ","Ô","Ố","Ồ", "Ộ","Ổ","Ỗ","Ơ","Ớ","Ờ","Ợ","Ở", "Ỡ"); 
		$uni[6] = array("ú","ù","ụ","ủ","ũ","ư","ứ","ừ", "ự","ử","ữ"); 
		$uni[7] = array("Ú","Ù","Ụ","Ủ","Ũ","Ư","Ứ","Ừ", "Ự","Ử","Ữ"); 
		$uni[8] = array("í","ì","ị","ỉ","ĩ"); 
		$uni[9] = array("Í","Ì","Ị","Ỉ","Ĩ"); 
		$uni[10] = array("đ"); 
		$uni[11] = array("Đ"); 
		$uni[12] = array("ý","ỳ","ỵ","ỷ","ỹ"); 
		$uni[13] = array("Ý","Ỳ","Ỵ","Ỷ","Ỹ"); 
		
		for($i=0; $i<=13; $i++) {
			$text = str_replace($uni[$i],$chars[$i],$text); 
		}
    return strtolower($text);
}
function isValidEmail($email){
	return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}
function validate_username($username){
    $check = eregi_replace('([a-zA-Z0-9_]+)', "", $username);
    if(empty($check)){
		return true;
    }else{
		return false;
    }
}

?>